package com.bocai.service.impl;

import com.bocai.bean.MessageInfo;
import com.bocai.bean.TUserInfo;
import com.bocai.mapper.MessageInfoMapper;
import com.bocai.mapper.TUserInfoMapper;
import com.bocai.service.IMessageInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * Created by jyf on 18/4/11.
 */
@Service
public class MessageInfoServiceImpl implements IMessageInfoService {

    @Autowired
    private MessageInfoMapper messageInfoMapper;

    @Autowired
    private TUserInfoMapper userInfoMapper;

    @Override
    public List<MessageInfo> selectAll(Integer userId) {
        if (null == userId || userId <= 0) {
            return messageInfoMapper.selectAll(0);
        } else {
            TUserInfo tUserInfo = new TUserInfo();
            tUserInfo.setUserId(userId);
            tUserInfo.setUserReadMessageState(1);
            userInfoMapper.updateByPrimaryKeySelective(tUserInfo);
            return messageInfoMapper.selectAll(userId);
        }
    }

    @Override
    public MessageInfo selectMessageInfo(Integer messageId) {
        return messageInfoMapper.selectByPrimaryKey(messageId);
    }

    @Override
    public int saveMessageInfo(MessageInfo messageInfo) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", messageInfo.getUserId());
        if (messageInfo.getUserId() == 0) {
            //代表系统插入消息 全部重置
            userInfoMapper.updateReadState(map);
        } else {
            // 代表用户个人插入  只需要自己的重置
            userInfoMapper.updateReadState(map);
        }
        return messageInfoMapper.insertSelective(messageInfo);
    }

    @Override
    public int updateMessageInfo(MessageInfo messageInfo) {
        return messageInfoMapper.updateByPrimaryKeySelective(messageInfo);
    }
}
